//Actual control of the ajax applicatio
var urlBase = "ajax/?";

function confirmAction(myMessage)
{   
    return confirm(myMessage);
}


function disableButton(id,message){
	document.getElementById(id).disabled = true;
	if(message){
	   document.getElementById(id).value = message;
	}
}


function unhide(elementName,advanced){
    if(document.getElementById(elementName)){
       if(advanced){
           var myEl = document.getElementById(elementName);
           //new Effect.Appear(myEl,{duration:0.3});
           new Effect.BlindDown(myEl,{duration:0.3});
       }else{
	       document.getElementById(elementName).style.display="";
       }
    }
	return false;
}


function hide(elementName,advanced){
    if(document.getElementById(elementName)){
       if(advanced){
	       var myEl = document.getElementById(elementName);
           //new Effect.Fade(myEl,{duration:0.3});
           new Effect.BlindUp(myEl,{duration:0.3});
       }else{
           document.getElementById(elementName).style.display="none";
       }
    }
	return false
}


// there are more than one functions called hide. for better distinguish
// we need hide2. 
function hide2(elementName,advanced){
    if(document.getElementById(elementName)){
       if(advanced){
	       var myEl = document.getElementById(elementName);
           //new Effect.Fade(myEl,{duration:0.3});
           new Effect.BlindUp(myEl,{duration:0.3});
       }else{
           document.getElementById(elementName).style.display="none";
       }
    }
	return false
}


function deleteme(id){
	var pulse = 0;
	var is_deleted = 0;
	var is_error = 0;
	
	todel = document.getElementById(id);

	var flash = function () {
		var rgb = hsv_to_rgb(0, Math.cos((pulse + 1) / 2), 1);
		pulse += 3.14159 / 5;
		var color = "rgb(" + rgb[0] + "," + rgb[1] + "," + rgb[2] + ")";

		todel.style.backgroundColor = color;

		if (is_error) {
			todel.style.border = "";
			// and let timer expire
		} else if (is_deleted) {
			//gone!
		} else {
			window.setTimeout(flash, 50);
		}
	};

	window.setTimeout(flash, 5);
}


function pause(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}


function hsv_to_rgb (h, s, v){
	if (s == 0) {
	v *= 255;
	return [v,v,v];
	}

	h *= 6;
	var i = Math.floor(h);
	var f = h - i;
	var p = v * (1 - s);
	var q = v * (1 - s * f);
	var t = v * (1 - s * (1 - f));

	v = Math.floor(v * 255 + 0.5);
	t = Math.floor(t * 255 + 0.5);
	p = Math.floor(p * 255 + 0.5);
	q = Math.floor(q * 255 + 0.5);

	if (i == 0) return [v,t,p];
	if (i == 1) return [q,v,p];
	if (i == 2) return [p,v,t];
	if (i == 3) return [p,q,v];
	if (i == 4) return [t,p,v];
	return [v,p,q];
}

/***
 * Deletes a row from the table (in fact will delete any element)
 *
 * @author Kulikov Alexey
 ***/
function deleteRow(id){
    if(document.getElementById(id)){
        var em=document.getElementById(id);
        em.parentNode.removeChild(em);
    }
}


var win = null;
function NewWindow(mypage,myname,w,h,scroll){
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable=1';
	var mywin = window.open(mypage,myname,settings);
	mywin.focus();
}



function isValidKey(string){
    var loginRegxp = /^([A-Za-z0-9_]){2,128}$/;
    return loginRegxp.test(string);
}